package com.ljx.itbbs.server.article.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.common.domain.enums.CommonStatusEnum;
import com.ljx.itbbs.server.article.domain.entity.PlateEntity;
import com.ljx.itbbs.server.article.domain.vo.req.admin.PlateAdminUpdateReq;
import com.ljx.itbbs.server.article.domain.vo.resp.PlateResp;
import com.ljx.itbbs.server.article.domain.vo.resp.admin.PlateAdminResp;
import com.ljx.itbbs.server.article.mapper.PlateMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PlateDao extends ServiceImpl<PlateMapper, PlateEntity> {

    public List<PlateResp> getAllPlate() {
        List<PlateEntity> plateEntityList = this.baseMapper.selectList(
                new LambdaQueryWrapper<PlateEntity>()
                        .eq(PlateEntity::getStatus, CommonStatusEnum.NORMAL.getStatus())
                        .orderByAsc(PlateEntity::getSort)
        );
        return plateEntityList.stream()
                .map(item -> {
                    PlateResp plateResp = BeanUtil.copyProperties(item, PlateResp.class);
                    plateResp.setUserList(JSONUtil.toList(item.getPlaterList(), PlateResp.UserInfo.class));
                    return plateResp;
                }).collect(Collectors.toList());
    }

    public List<PlateAdminResp> adminPlateList() {
        return BeanUtil.copyToList(this.baseMapper.selectList(
                new LambdaQueryWrapper<PlateEntity>()
                        .orderByAsc(PlateEntity::getSort)
        ), PlateAdminResp.class);
    }

    public void adminUpdatePlate(PlateAdminUpdateReq req) {
        lambdaUpdate().set(PlateEntity::getName, req.getName())
                .set(PlateEntity::getStatus, req.getSort())
                .set(PlateEntity::getStatus, req.getStatus())
                .eq(PlateEntity::getId, req.getId())
                .update();
    }
}
